home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
PROGRAMR
/
OLE2BOOK.ZIP
/
CHAP08.ZIP
/
CHAP08
/
PATRON
/
PAGE.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1993-06-17
|
22KB
|
921 lines
/*
* PAGE.CPP
* Modifications for Chapter 8: None
*
* Implementation of parts of the CPage class; those member functions
* dealing with mouse events are in PAGEMOUS.CPP
*
* Copyright (c)1993 Microsoft Corporation, All Rights Reserved
*
* Kraig Brockschmidt, Software Design Engineer
* Microsoft Systems Developer Relations
*
* Internet : kraigb@microsoft.com
* Compuserve: >INTERNET:kraigb@microsoft.com
*/
#include "patron.h"
/*
* CPage::CPage
* CPage::~CPage
*
* Constructor Parameters:
* dwID DWORD identifier for this page.
* hWnd HWND of the pages window (for repaints, etc).
* pPG LPCPages to the Pages window.
*/
CPage::CPage(DWORD dwID, HWND hWnd, LPCPages pPG)
{
m_dwID =dwID;
m_pIStorage=NULL;
m_cOpens=0;
m_hWnd=hWnd;
m_pPG=pPG;
m_dwIDNext =0;
m_cTenants =0;
m_hWndTenantList=NULL;
m_iTenantCur =0xFFFF; //Tenants are zero indexed.
m_pTenantCur =NULL;
m_uHTCode=HTNOWHERE;
m_uSizingFlags=0;
m_fTracking=FALSE;
m_hDC=NULL;
return;
}
CPage::~CPage(void)
{
m_hWnd=NULL;
Close(FALSE);
return;
}
/*
* CPage::GetID
*
* Return Value:
* DWORD dwID field in this page. This function is only here
* to avoid hiding inline implementations in pages.h
*/
DWORD CPage::GetID(void)
{
return m_dwID;
}
/*
* CPage::FOpen
*
* Purpose:
* Retrieves the IStorage associated with this page. The IStorage is
* owned by the page and thus the page always holds a reference count.
* The caller should call ::Close or delete this page to match this open.
*
* This function may be called multiple times resulting in additional
* reference counts on the storage each of which must be matched with
* a call to ::Close. The last ::Close can be done through delete.
*
* Parameters:
* pIStorage LPSTORAGE in which this page lives.
*
* Return Value:
* BOOL TRUE if opening succeeds, FALSE otherwise.
*/
BOOL CPage::FOpen(LPSTORAGE pIStorage)
{
HRESULT hr=NOERROR;
LPSTREAM pIStream;
DWORD dwMode;
char szTemp[32];
BOOL fNew;
BOOL fCreated=FALSE;
TENANTLIST tl;
LPTENANTINFO pti;
ULONG cb;
LPMALLOC pIMalloc;
UINT i;
LPTENANT pTenant;
fNew=(NULL==m_pIStorage);
if (!fNew)
{
m_cOpens++;
m_pIStorage->AddRef();
return TRUE;
}
if (NULL==pIStorage)
return FALSE;
/*
* Attempt to open the storage under this ID. If there is none, then
* create it. In either case we end up with an IStorage that we
* either save in pPage or release.
*/
GetStorageName(szTemp);
dwMode=STGM_TRANSACTED | STGM_READWRITE | STGM_SHARE_EXCLUSIVE;
hr=pIStorage->OpenStorage(szTemp, NULL, dwMode, NULL, 0, &m_pIStorage);
if (FAILED(hr))
{
hr=pIStorage->CreateStorage(szTemp, dwMode, 0, 0, &m_pIStorage);
fCreated=TRUE;
}
if (FAILED(hr))
return FALSE;
m_cOpens++;
if (NULL==m_hWndTenantList)
{
/*
* The first time we open this page, create the hidden listbox
* we'll use to track tenants. We give it the owner-draw style
* so we can just store pointers in it.
*/
m_hWndTenantList=CreateWindow("listbox", "Tenant List"
, WS_POPUP | LBS_OWNERDRAWFIXED, 0, 0, 100, 100
, HWND_DESKTOP, NULL, m_pPG->m_hInst, NULL);
if (NULL==m_hWndTenantList)
return FALSE;
}
//If this is brand-new, we're done.
if (fCreated)
return TRUE;
/*
* Now open the stream we saved in ::Close and load all the
* tenants listed in there. If there are none, then we don't
* have to load squat.
*/
hr=m_pIStorage->OpenStream(SZSTREAMTENANTLIST, NULL, STGM_DIRECT
| STGM_READ | STGM_SHARE_EXCLUSIVE, 0, &pIStream);
if (FAILED(hr))
return FALSE;
if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
{
pIStream->Read((LPVOID)&tl, sizeof(tl), NULL);
m_cTenants=tl.cTenants;
m_dwIDNext=tl.dwIDNext;
m_iTenantCur=0;
cb=tl.cTenants*sizeof(TENANTINFO);
if (0!=cb)
{
pti=(LPTENANTINFO)pIMalloc->Alloc(cb);
if (NULL!=pti)
{
pIStream->Read((LPVOID)pti, cb, NULL);
for (i=0; i < m_cTenants; i++)
{
if (FTenantAdd(-1, (pti+i)->dwID, &pTenant))
pTenant->FLoad(m_pIStorage, &(pti+i)->fe, &(pti+i)->rcl);
}
pIMalloc->Free((LPVOID)pti);
}
}
pIMalloc->Release();
}
pIStream->Release();
//Get and select the first tenant
if (FTenantGet(0, &m_pTenantCur, FALSE))
m_pTenantCur->Select(TRUE);
return TRUE;
}
/*
* CPage::Close
*
* Purpose:
* Possibly commits the storage, then releases it reversing the
* reference count from FOpen.
*
* Parameters:
* fCommit BOOL indicating if we're to commit.
*
* Return Value:
* None
*/
void CPage::Close(BOOL fCommit)
{
if (NULL==m_pIStorage)
return;
if (fCommit)
Update();
m_pIStorage->Release();
//If this was the last close, make all tenants loaded->passive
if (0==--m_cOpens)
{
UINT i;
LPTENANT pTenant;
m_pIStorage=NULL;
for (i=0; i < m_cTenants; i++)
{
if (FTenantGet(i, &pTenant, FALSE))
{
if (NULL!=m_hWnd)
{
//FOpen may select again, so this repaints.
pTenant->Select(FALSE);
}
pTenant->Close(FALSE);
delete pTenant;
}
}
DestroyWindow(m_hWndTenantList);
m_hWndTenantList=NULL;
}
return;
}
/*
* CPage::Update
*
* Purpose:
* Forces a common on the page if it's open.
*
* Parameters:
* None
*
* Return Value:
* BOOL TRUE if there are any open objects on this page,
* that is, we should remain open.
*/
BOOL CPage::Update(void)
{
BOOL fOpen=FALSE;
UINT i;
LPTENANT pTenant;
HRESULT hr;
LPSTREAM pIStream;
TENANTLIST tl;
LPTENANTINFO pti;
ULONG cb;
LPMALLOC pIMalloc;
//Walk the list of objects and update them all as well.
for (i=0; i < m_cTenants; i++)
{
if (FTenantGet(i, &pTenant, FALSE))
fOpen |= pTenant->Update();
}
//Now write our own stream containing the tenant list.
hr=m_pIStorage->CreateStream(SZSTREAMTENANTLIST, STGM_CREATE | STGM_WRITE
| STGM_DIRECT | STGM_SHARE_EXCLUSIVE, 0, 0, &pIStream);
if (FAILED(hr))
return fOpen;
if (SUCCEEDED(CoGetMalloc(MEMCTX_SHARED, &pIMalloc)))
{
tl.cTenants=m_cTenants;
tl.dwIDNext=m_dwIDNext;
pIStream->Write((LPVOID)&tl, sizeof(TENANTLIST), &cb);
cb=m_cTenants*sizeof(TENANTINFO);
pti=(LPTENANTINFO)pIMalloc->Alloc(cb);
if (NULL!=pti)
{
for (i=0; i < m_cTenants; i++)
{
FTenantGet(i, &pTenant, FALSE);
(pti+i)->dwID=pTenant->GetID();
pTenant->RectGet(&(pti+i)->rcl, FALSE);
pTenant->FormatEtcGet(&(pti+i)->fe, FALSE);
}
pIStream->Write((LPVOID)pti, cb, &cb);